<template>
	<view>
		<view class="content">
			<view class="list">
				<view class="row" v-for="(item,index) in list" :key="index" @click="selectAddress(item)">
					<view class="left">
						<view class="head">
							{{item.userName.substr(0,1)}}
						</view>
					</view>
					<view class="center">
						<view class="name-tel">
							<view class="name">{{item.userName}}</view>
							<view class="tel">{{item.phone}}</view>
							<view class="default" v-if="item.defaultFlag==1">
								默认
							</view>
						</view>
						<view class="address">
							{{item.locationAddress}}（{{item.addressDetail}}）
						</view>
					</view>
					<view class="right">
						<view class="icon ri-edit-fill" @tap.stop="editAddress(item)"></view>
					</view>
				</view>
			</view>
		</view>
		<view class="add">
			<view class="button" @tap="editAddress">
				新增地址
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				list:[],
				type:0
			}
		},
		onLoad(e) {
			e.type?this.type=1:this.type=0
		},
		onShow(e) {
		   this.myUserAddress()	
		},
		methods: {
			editAddress(data){
				data?uni.navigateTo({
					url:"./edit?item="+ JSON.stringify(data)
				}):uni.navigateTo({
					url:"./edit"
				})
				
			},
			myUserAddress(){
				this.$api("address",'get').then(res=>{
					this.list=res.data
				})
			},
			deleteAddress(item){
				let self=this;
				uni.showModal({
					title: '删除',
					    content: '确定删除这个地址',
					    success: function (res) {
						if (res.confirm) {
							self.$api("address",'del',{id:item._id}).then(res=>{
								self.$msg("删除成功");
								self.myUserAddress();
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			selectAddress(data){
				if(this.type){
					getApp().globalData.defaultAddress=data
					uni.navigateBack();
				}else{
					uni.navigateTo({
						url:"./edit?item="+ JSON.stringify(data)
					})
				}
			},
			async setAddress(item){
				await this.$api("address","clearDefault")
				item.defaultFlag=1
				await this.$api("address","upd",item)
				if(this.type){
					getApp().globalData.defaultAddress=item
					uni.navigateBack();
				}else{
					await this.myUserAddress()
					this.$msg("设置成功");
				}
			}
		}
	}
</script>

<style lang="scss">
view{
	display: flex;
}
	.icon {
		// &.bianji {
		// 	&:before{content:"\e61b";}
		// }
		// &.tianjia {
		// 	&:before{content:"\e81a";}
		// }
	}
	.add{
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		justify-content: center;
		align-items: center;
		.button{
			width: 90%;
			    text-align: center;
			    display: block;
			margin-bottom:20px ;
			background-image: linear-gradient(-90deg, 
			#3da180 0%, 
			#62c980 100%), 
			linear-gradient(
			#e8ecef, 
			#e8ecef);
			background-blend-mode: normal, 
			normal;
			border-radius: 25px;
			
			height: 45px;
			line-height: 45px;
			font-family: PingFangSC-Medium;
			font-size: 14px;
			letter-spacing: 0px;
			color: #ffffff;
		}
	}
	.list{
		flex-wrap: wrap;
		.row{
			width: 96%;
			padding: 20upx 2%;
			.left{
				width: 90upx;
				flex-shrink: 0;
				align-items: center;
				.head{
					width: 70upx;
					height: 70upx;
					background:linear-gradient(to right,#ccc,#aaa);
					color: #fff;
					justify-content: center;
					align-items: center;
					border-radius: 60upx;
					font-size: 35upx;
				}
			}
			.center{
				width: 100%;
				flex-wrap: wrap;
				.name-tel{
					width: 100%;
					align-items: baseline;
					.name{
						font-size: 34upx;
					}
					.tel{
						margin-left: 30upx;
						font-size: 24upx;
						color: #777;
					}
					.default{

						font-size: 22upx;
						
						background-color: #62c980;
						color: #fff;
						padding: 0 18upx;
						border-radius: 24upx;
						margin-left: 20upx;
					}
				}
				.address{
					width: 100%;
					font-size: 24upx;
					align-items: baseline;
					color: #777;
				}
			}
			.right{
				flex-shrink: 0;
				align-items: center;
				margin-left: 20upx;
				.icon{
					justify-content: center;
					align-items: center;
					width: 80upx;
					height: 60upx;
					border-left: solid 1upx #aaa;
					font-size: 40upx;
					color: #777;
				}
			}
		}
	}
</style>
